Recibo errores en mi leetcode y no estoy seguro de por qué:
var addTwoNumbers = function(l1, l2) { let newL1 = [] let newL2 = [] let answer = [] for(let i = 0; i < l1.length; i++) { newL1[i] = l1[l1.length - 1 - i] } for(let i = 0; i < l2.length; i++) { newL2[i] = l2[l2.length - 1 - i] } let num = parseInt(newL1.toString().replace(/,/g, '')) + parseInt(newL2.toString().replace(/,/g, '')) let rawAnswer = (num.toString().split("")) for(let i=0; i < rawAnswer.length; i++) { answer[i] = parseInt(rawAnswer[i]) } return answer}
Error:
Line 45 in solution.js throw new TypeError(__serialize__(ret) + " is not valid value for the expected return type ListNode"); ^ TypeError: null is not valid value for the expected return type ListNode Line 45: Char 20 in solution.js (Object.<anonymous>) Line 16: Char 8 in runner.js (Object.runner) Line 29: Char 26 in solution.js (Object.<anonymous>) Line 1251: Char 30 in loader.js (Module._compile) Line 1272: Char 10 in loader.js (Object.Module._extensions..js) Line 1100: Char 32 in loader.js (Module.load) Line 962: Char 14 in loader.js (Function.Module._load) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) Line 17: Char 47 in run_main_module.jsDescripción del desafío:
Se le dan dos listas enlazadas no vacías que representan dos enteros no negativos. Los dígitos se almacenan en orden inverso y cada uno de sus nodos contiene un solo dígito. Sume los dos números y devuelva la suma como una lista enlazada. Puede suponer que los dos números no contienen ningún cero inicial, excepto el propio número 0.
Ejemplo:
Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.No estoy seguro de por qué recibo este error, pero sé que estoy haciendo algo que no le gusta a leetcode.
Gracias
La descripción dice: return the sum as a linked list
Está haciendo dos parseInt y devolviendo la suma (que es un número), pero en su lugar debería devolver una lista vinculada, definida por el encabezado de la lista (el primer objeto ListNode .
var addTwoNumbers = function(l1, l2) { let k=new ListNode(0,null); let k1= k; let x,y,c=0; while(l1!=null || l2!=null){ x = l1!=null?l1.val:0; y = l2!=null?l2.val:0; c=x+y+c; k1.next =new ListNode(c%10,null); k1 = k1.next ; c=parseInt(c/10); if(l1!=null) l1=l1.next ; if(l2!=null) l2=l2.next ; } if(c>0) { k1.next=new ListNode(c,null); k1=k1.next ; } return k.next; };ListNode esta función.